import {
  DATE_CUTS,
  FORMAT_DATE,
  INIT_SEARCH_TIME,
  GET_USER_IDS,
  IS_EXPIRE_BOOLEAN
} from '@/utils/const';
import userStore from '@/store/modules/user';
import auth from '@/plugins/auth';

// 注入组件内数据
const componentData = ref({});
export const injectData = obj => {
  componentData.value = { ...componentData.value, ...obj };
};

// 表格选择
export const selectList = ref([]);

// 查询表单
export const searchParams = reactive({
  domain_usage_map: '-1'
});

export const resetFormData = reactive({
  domain_usage_map: '-1'
});

watch(
  () => searchParams.applyRange,
  val => {
    INIT_SEARCH_TIME(searchParams, val, 'begin_at', 'end_at');
  },
  { immediate: true }
);
watch(
  () => searchParams.domain_usage_map,
  val => {
    if (!val || val === '-1') searchParams.domain_usage = undefined;
    else searchParams.domain_usage = val;
  },
  { immediate: true }
);

// 查询条件
export const formItems = reactive({
  domain: {
    component: 'input',
    label: '域名',
    placeholder: '请输入域名',
    maxlength: 100
  },
  business_group_id: {
    component: 'select',
    filterable: true,
    label: '业务组',
    clearable: false,
    options: []
  },
  user_name: {
    component: 'input',
    label: '使用人员',
    placeholder: '请输入使用人员',
    showOnly: true,
    maxlength: 100,
    input() {
      searchParams.user_ids = GET_USER_IDS(searchParams.user_name);
    }
  },
  site_type: {
    component: 'select',
    filterable: true,
    label: '站点类型',
    clearable: false,
    options: []
  },
  domain_usage_map: {
    component: 'cascader',
    label: '域名用途',
    clearable: false,
    showAllLevels: false,
    showOnly: true,
    props: { emitPath: false, label: 'name', value: 'id', children: 'childs' },
    options: []
  },
  applyRange: {
    component: 'date-picker',
    type: 'daterange',
    label: '创建时间',
    clearable: true,
    showOnly: true,
    shortcuts: [
      { text: '最近7天', value: () => DATE_CUTS.LAST_7_DAYS() },
      { text: '本周', value: () => DATE_CUTS.THIS_WEEK() },
      { text: '本月', value: () => DATE_CUTS.THIS_MONTH() },
      { text: '上周', value: () => DATE_CUTS.PREV_WEEK() },
      { text: '上月', value: () => DATE_CUTS.PREV_MONTH() },
      { text: '最近31天', value: () => DATE_CUTS.LAST_31_DAYS() },
      { text: '最近180天', value: () => DATE_CUTS.LAST_180_DAYS() }
    ]
  },
  is_due_soon: {
    component: 'select',
    filterable: true,
    label: '是否即将到期',
    clearable: false,
    options: [{ label: '全部', value: undefined }, ...IS_EXPIRE_BOOLEAN]
  }
});

// 表格字段
export const columns = ref([
  {
    type: 'selection',
    fixed: 'left',
    width: '50',
    reserveSelection: true,
    selectable(row) {
      if (!selectList.value.length) return true;
      return selectList.value[0]?.group?.id === row?.group?.id;
    }
  },
  { prop: 'domain', label: '域名', fixed: 'left' },
  {
    prop: 'business_group',
    label: '业务组',
    fixed: 'left',
    format(data) {
      return data?.name;
    }
  },
  {
    prop: 'group',
    label: '所属团队',
    format(data) {
      return data?.name;
    }
  },
  {
    prop: 'user_id',
    label: '使用人员',
    format(data) {
      return userStore().userMap[data]?.label;
    }
  },
  {
    prop: 'site_type',
    label: '站点类型',
    format(data) {
      return data?.name;
    }
  },
  {
    prop: 'expiration_time',
    slot: 'expiration_time',
    label: '到期时间(UTC+8)'
  },
  {
    prop: 'updater_id',
    label: '更新人',
    width: '120',
    format(data) {
      return userStore().userMap[data]?.label;
    }
  },
  {
    prop: 'updated_at',
    label: '更新时间',
    format(data) {
      return FORMAT_DATE(data);
    }
  },
  {
    label: '操作',
    width: '120px',
    fixed: 'right',
    buttons: [
      {
        label: '查看',
        click({ row }) {
          componentData.value.handleDetail(row);
        }
      },
      {
        label: '弃用',
        type: 'danger',
        hidden() {
          // 需要判断权限
          return !auth.hasPermi('resources:sites:giveup');
        },
        click({ row }) {
          componentData.value.giveUp(row);
        }
      }
    ]
  }
]);
